##This file generates all of the figures. 
##Clearing all plots may be necessary for the next plot to appear correctly. 
##Note the manual changes to plot width and height in brackets

library(foreign)

conquestsdat<-read.dta("Conquest REP.dta")
conquestsdat<-subset(conquestsdat,entire==1)
conquestsdat<-subset(conquestsdat,retaliatory==0)
grabsonly<-read.dta("Conquest REP.dta")
grabsonly<-subset(grabsonly,entire==0)
grabsonly<-subset(grabsonly,retaliatory==0)

# This dataset is used to determine the number of states in the international system. 
# Missing data was entered manually for the most recent years.
isd<-read.csv("ISD.csv")
isd<-subset(isd,Year>1917)

gar<-subset(grabsonly, garrison==1)
ungar<-subset(grabsonly, garrison==0)
popul2<-subset(grabsonly, popul==2)
popul1<-subset(grabsonly, popul==1)
unpopul<-subset(grabsonly, popul==0)

conquests<-conquestsdat$year
total<-c(conquests,grabsonly$year)
allgrabs<-grabsonly$year
allgrabsSO<-allgrabs[grabsonly$held==1]
conquestsSO<-conquests[conquestsdat$held==1]
gar<-gar$year
ungar<-ungar$year
popul2<-popul2$year
popul1<-popul1$year
unpopul<-unpopul$year

conquestsx<-as.data.frame(table(factor(conquests,1918:2018)))
totalx<-as.data.frame(table(factor(total,1918:2018)))
allgrabsx<-as.data.frame(table(factor(allgrabs,1918:2018)))
allgrabsSOx<-as.data.frame(table(factor(allgrabsSO,1918:2018)))
conquestsSOx<-as.data.frame(table(factor(conquestsSO,1918:2018)))
garx<-as.data.frame(table(factor(gar,1918:2018)))
ungarx<-as.data.frame(table(factor(ungar,1918:2018)))
popul2x<-as.data.frame(table(factor(popul2,1918:2018)))
popul1x<-as.data.frame(table(factor(popul1,1918:2018)))
unpopulx<-as.data.frame(table(factor(unpopul,1918:2018)))

conquestsadj<-as.data.frame(table(factor(conquests,1918:2018)))
conquestsadj$Freq<-conquestsadj$Freq/isd$Total
allgrabsadj<-as.data.frame(table(factor(allgrabs,1918:2018)))
allgrabsadj$Freq<-allgrabsadj$Freq/isd$Total

sp<-.6
conquestslo<-loess(as.integer(Freq)~as.integer(Var1),conquestsx,span=sp)
totallo<-loess(as.integer(Freq)~as.integer(Var1),totalx,span=sp)
allgrabslo<-loess(as.integer(Freq)~as.integer(Var1),allgrabsx,span=sp)
conquestsSOlo<-loess(as.integer(Freq)~as.integer(Var1),conquestsSOx,span=sp)
allgrabsSOlo<-loess(as.integer(Freq)~as.integer(Var1),allgrabsSOx,span=sp)
garlo<-loess(as.integer(Freq)~as.integer(Var1),garx,span=sp)
ungarlo<-loess(as.integer(Freq)~as.integer(Var1),ungarx,span=sp)
popul2lo<-loess(as.integer(Freq)~as.integer(Var1),popul2x,span=sp)
popul1lo<-loess(as.integer(Freq)~as.integer(Var1),popul1x,span=sp)
unpopullo<-loess(as.integer(Freq)~as.integer(Var1),unpopulx,span=sp)
conquestsadjlo<-loess((Freq)~as.integer(Var1),conquestsadj,span=sp)
allgrabsadjlo<-loess((Freq)~as.integer(Var1),allgrabsadj,span=sp)

##Figure 1 Gray [width 900]
par(mar=c(7.1,7.1,3.1,7.1))
plot(predict(conquestslo),xlim=c(12,87),xaxt="n",yaxs="i",ylim=c(-.03,1.6),
     ylab="Conquest Attempts Per Year",xlab=NA,
     col="black",type="l",cex=2,lwd=2,
     main="Territorial Conquest, 1930-2005")
lines(predict(allgrabslo),col="black",cex=2,lwd=2,lty=2)
axis(1,at=c(12,22,32,42,52,62,72,82),cex=2,
     labels=c(1930,1940,1950,1960,1970,1980,1990,2000))
legend("bottom",legend=c("Entire States",
                         "Parts of States"),horiz=TRUE,
       lty=c(1,2),col=c("black","black"),xpd=NA,inset=-.38)

##Figure 2 Gray [width 900]
par(mar=c(7.1,7.1,3.1,7.1))
plot(predict(conquestsSOlo),xlim=c(12,87),xaxt="n",yaxs="i",ylim=c(-.03,.8),
     ylab="Conquest Attempts Per Year",xlab=NA,
     col="black",type="l",cex=2,lwd=2,
     main="Territorial Conquest, 1930-2005")
lines(predict(allgrabsSOlo),col="black",cex=2,lwd=2,lty=2)
axis(1,at=c(12,22,32,42,52,62,72,82),cex=2,
     labels=c(1930,1940,1950,1960,1970,1980,1990,2000))
legend("bottom",legend=c("Entire States",
                         "Parts of States"),horiz=TRUE,
       lty=c(1,2),col=c("black","black"),xpd=NA,inset=-.38)

##Figure 3 Gray [width 900]
par(mar=c(7.1,7.1,3.1,7.1))
plot(predict(conquestsadjlo),xlim=c(12,87),xaxt="n",yaxs="i",ylim=c(-.0015,.015),
     ylab="Conquest Attempts Per State",xlab=NA,
     col="black",type="l",cex=2,lwd=2,
     main="Territorial Conquest, 1930-2005")
lines(predict(allgrabsadjlo),col="black",cex=2,lwd=2,lty=2)
axis(1,at=c(12,22,32,42,52,62,72,82),cex=2,
     labels=c(1930,1940,1950,1960,1970,1980,1990,2000))
legend("bottom",legend=c("Entire States",
                         "Parts of States"),horiz=TRUE,
       lty=c(1,2),col=c("black","black"),xpd=NA,inset=-.38)

##Figure 4 Territory Sizes [width 900]
table(grabsonly$size)
levs2<-c(sum(na.omit(grabsonly$size==.5)),sum(na.omit(grabsonly$size==1)),
         (sum(na.omit(grabsonly$size==1.5))+sum(na.omit(grabsonly$size==2))),
         (sum(na.omit(grabsonly$size==2.5))+sum(na.omit(grabsonly$size==3))),
         (sum(na.omit(grabsonly$size==3.5))+sum(na.omit(grabsonly$size==4))),
         sum(na.omit(grabsonly$size>4)),0,0,sum(conquestsdat$completed==0),
         sum(conquestsdat$completed==1))

barplot(levs2,col=c("dark gray"),ylab="Frequency",
        xlab="Number of Provinces Occupied",
        names.arg=c("<1","1","2","3","4","5","6","7-9","10+","Entire"))

##Populated (Figure 5) [width 1100, height 400]
####Clearing all plots may be necessary for this plot to appear correctly. 

popvec<- c(mean(grabsonly$cowwar[grabsonly$popul==2]),
           mean(grabsonly$cowwar[grabsonly$popul==1]),
           mean(grabsonly$cowwar[grabsonly$popul==0]))

layout(matrix(c(1,2), 1, 2, byrow = TRUE),widths=c(2,1))

plot(predict(popul2lo),xlim=c(12,87),xaxt="n",yaxs="i",ylim=c(-.03,1.03),
     ylab="Conquest Attempts Per Year",xlab=NA,lty=3,
     col="black",type="l",cex=2,lwd=2)
lines(predict(popul1lo),col="black",cex=2,lwd=2,lty=2)
lines(predict(unpopullo),col="black",cex=2,lwd=2,lty=1)
axis(1,at=c(12,22,32,42,52,62,72,82),cex=2,
     labels=c(1930,1940,1950,1960,1970,1980,1990,2000))
legend("bottom",legend=c("Major City","Populated",
                         "Unpopulated"),horiz=TRUE,
       lty=c(3,2,1),col=c("black","black","black"),xpd=NA,inset=-.38)

barplot(popvec,col=c("light gray"),ylim=c(0,1),ylab="Probability of War",
        names.arg=c("M. City","Popul.","Unpopul."))

##Garrisoned (Figure 6) [width 1100, height 400]
####Clearing all plots may be necessary for this plot to appear correctly. 

garvec<- c(mean(grabsonly$cowwar[grabsonly$garrison==0]),
           mean(grabsonly$cowwar[grabsonly$garrison==1]))

layout(matrix(c(1,2), 1, 2, byrow = TRUE),widths=c(2,1))

plot(predict(garlo),xlim=c(12,87),xaxt="n",yaxs="i",ylim=c(-.03,1.03),
     ylab="Conquest Attempts Per Year",xlab=NA,
     col="black",type="l",cex=2,lwd=2,lty=2)
lines(predict(ungarlo),col="black",cex=2,lwd=2,lty=1)
axis(1,at=c(12,22,32,42,52,62,72,82),cex=2,
     labels=c(1930,1940,1950,1960,1970,1980,1990,2000))
legend("bottom",legend=c("Undefended",
                         "Garrisoned"),horiz=TRUE,
       lty=c(1,2),col=c("black","black"),xpd=NA,inset=-.38)

barplot(garvec,col=c("light gray"),ylim=c(0,1),ylab="Probability of War",
        names.arg=c("Undefended","Garrisoned"))

